package com.thinking.dubbo.api;

import com.thinking.dubbo.common.LanguageEnum;
import com.thinking.dubbo.common.ResponseCodeI18n;
import com.thinking.dubbo.common.TtlThreadUtil;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;

/**
 * @ClassName ApiResultI18n
 * @Description TODO
 * @Author tangsixiang@gmail.com
 * @Date 2023/6/5 8:59
 * @Version 1.0
 */
@Data
public class ResultI18nResponse implements Serializable {

    /** * 返回码，200 正常 */
    private int code = 200;
    /** * 返回信息 */
    private String msg = "成功-默认";
    /** * 返回数据 */
    private Object data;
    /** * api 返回结果 */
    private ResultI18nResponse() { }
    public ResultI18nResponse(String language){
        this.code = ResponseCodeI18n.SUCCESS.getCode();
         try {
            this.msg = I18nMessageUtil.getMessage(LanguageEnum.getLanguageType(language),code);
        } catch (IOException e) {
            e.printStackTrace();
            this.msg = "SUCCESS";
        }
    }

    public ResultI18nResponse(String language,int code, Object data) {
      this.code=code;
        try {
            this.msg = I18nMessageUtil.getMessage(LanguageEnum.getLanguageType(language),code);
        } catch (IOException e) {
            e.printStackTrace();
            this.msg = "COMMON";
        }
        this.data = data;
    }

    public ResultI18nResponse(int code, Object data) {
        this.code = code;
        try {
            this.msg = I18nMessageUtil.getMessage(code);
        } catch (IOException e) {
            e.printStackTrace();
            this.msg = "COMMON";
        }
        this.data = data;
    }

    public ResultI18nResponse(int code) {
        this.code = code;
        try {
            this.msg = I18nMessageUtil.getMessage(code);
        } catch (IOException e) {
            e.printStackTrace();
            this.msg = "COMMON";
        }
    }

    /** * 获取成功状态结果,区分多语言(默认简体中文) * * @param language 语言类型,eg: en_us 表示美式英文 * @return */
    public static ResultI18nResponse success(String language) {
        return success(null, language);
    }
    /** * 获取成功状态结果,区分多语言(默认简体中文) * * @param data 返回数据 * @param language 语言类型,eg: en_us 表示美式英文 * @return */
    public static ResultI18nResponse success(Object data, String language) {
        ResultI18nResponse result = new ResultI18nResponse(language);
        result.setData(data);
        return result;
    }
    public static ResultI18nResponse success(Object data, String language,Integer code) {
        return new ResultI18nResponse(language,code,data);
    }
    public static ResultI18nResponse common(Object data, String language,Integer code) {
        return new ResultI18nResponse(language,code,data);
    }
    /** * 获取失败状态结果,区分多语言(默认简体中文) * * @param language 语言类型,eg: en_us 表示美式英文 * @return */
    public static ResultI18nResponse failure(String language) {
        return failure(ResponseCodeI18n.FAIL.getCode(), ResponseCodeI18n.FAIL.getMsg(), null, language);
    }
    /** * 获取失败结果,区分多语言(默认中文) * * @param responseCodeI18n 返回码 * @param language 语言类型 * @return */
    public static ResultI18nResponse failure(ResponseCodeI18n responseCodeI18n, String language) {
        return failure(responseCodeI18n.getCode(), responseCodeI18n.getMsg(), null, language);
    }
    /** * 获取失败状态结果,区分多语言(默认中文) * * @param code 返回状态码 * @param msg 错误信息 * @param language 语言类型,eg: en 表示英文 * @return */
    public static ResultI18nResponse failure(int code, String msg, String language) {
        return failure(code ,msg, null, language);
    }
    public static ResultI18nResponse failure(int code, String msg ) {
        return failure(code ,msg, null, null);
    }
    public static ResultI18nResponse error(Exception e)   {
        ResultI18nResponse result = new ResultI18nResponse( );
        result.setCode(500);
        result.setMsg(e.getMessage());
        return  result;

    }
    /** * 获取失败返回结果,区分多语言(默认中文) * * @param code 错误码 * @param msg 错误信息 * @param data 返回结果 * @param language 语言类型,eg: en 表示英文 * @return */
    public static ResultI18nResponse failure(int code, String msg, Object data, String language) {
        ResultI18nResponse result = new ResultI18nResponse(language);

        try {
            if(language.equals(null)){
                msg= I18nMessageUtil.getMessage(code);
            }else {
                msg= I18nMessageUtil.getMessage(language,code);
            }

        } catch (IOException e) {
            msg = "Error";
        }
        result.setCode(code);
        result.setMsg(msg);
        result.setData(data);
        if (data instanceof String) {
            String m = (String) data;
            if (!m.matches("^.*error$")) {
                m += "error";
            }
        }
        return result;
    }
}
